local a 50
set more off
local y 2008

*** OUTCOMES FROM FLAT FILE FOR AGE 50

use "Data/NCDS/UKDA-6137-stata9_se/stata9_se/ncds_2008_followup.dta", clear
merge  1:1 NCDSID using scratchdata/ncds_2008_unfolding.dta, nogenerate

* merge RPI
ge year=`y'
merge m:1 year using scratchdata/RPI.dta, keep(match) nogenerate

* sex
ge sex_`a'=ND8SEX 

** Observation indicator
ge Obs_50=1

rename N8REGION region_`a'

********************************************************************************
* Part 1: pay period for employed
********************************************************************************

* net
ge pp_nee=N8CNETPD if N8CNETPD>0
recode pp_nee (3=4) (4=4.33) (5=52) (6=.)

* gross
ge pp_gee=N8CGROPD if  N8CGROPD>0
recode pp_gee (3=4) (4=4.33) (5=52) (6=.)

********************************************************************************
* Part 2: pay for employed
********************************************************************************

* gross
ge gpee_`a'=N8CGROPY if N8CGROPY>=0

* net
ge npee_`a'=N8CNETPY if N8CNETPY>=0

********************************************************************************
* Part 3: pay per week for employed
********************************************************************************

* gross
replace gpee_`a'=gpee_`a'/pp_gee

* net
replace npee_`a'=npee_`a'/pp_nee

********************************************************************************
* Part 4: hours per week for employed
********************************************************************************

ge hoursee1=N8CHOUR1 if N8CHOUR1>=0
ge hoursee2=N8CHOUR2 if N8CHOUR2>=0
ge hoursee3=N8CHOUR3 if N8CHOUR3>=0

egen  hoursee_`a'=rowtotal(hoursee*), missing
drop hoursee1-hoursee3

********************************************************************************
* Part 5: weekly earnings for self-employed
********************************************************************************

* gross earnings in the last 12 months
ge ese_`a'=N8SEEARN if N8SEEARN>=0

* net profit within last 12 months
ge npse_`a'=N8SEPRIT if N8SEPRIT>=0

* self-employed: period for earnings 
ge sy=N8SEERYR if N8SEERYR>0
recode sy (9998 9999=.)

ge sm=N8SEERMO if N8SEERMO>0
recode sm (98 99=.)

ge ey=N8SERNYR if N8SERNYR>0
recode ey (9998 9999=.)

ge em=N8SERNMO if N8SERNMO>0
recode em (98 99=.)

ge pp_ese=ey+(em-1)/12-(sy+(sm-1)/12)

replace pp_ese=. if ese_`a'==.
replace pp_ese=1 if ese_`a'==0
replace pp_ese=1 if pp_ese==0
replace pp_ese=. if pp_ese<0
replace pp_ese=1 if pp_ese>1 & pp_ese!=.
drop ey em sy sm

* self-employed: period for net profit 
ge sy=N8SEPRYR if N8SEPRYR>0  
recode sy (9998 9999=.)

ge sm=N8SEPRMO if N8SEPRMO>0
recode sm (98 99=.)

ge ey=N8SERFYR if N8SERFYR>0
recode ey (9998 9999=.)

ge em=N8SERFMO if N8SERFMO>0
recode em (98 99=.)

ge pp_npse=ey+(em-1)/12-(sy+(sm-1)/12)
replace pp_npse=. if npse_`a'==.
replace pp_npse=1 if npse_`a'==0
replace pp_npse=1 if pp_npse==0
replace pp_npse=. if pp_npse<0
replace pp_npse=1 if pp_npse>1 & pp_npse!=.
drop ey em sy sm

* convert se incomes to year
replace ese_`a'=ese_`a'/pp_ese
drop pp_ese

replace npse_`a'=npse_`a'/pp_npse
drop pp_npse

********************************************************************************
* Merge earnings information from unfolding brackets
********************************************************************************

local vars g n
foreach v of local vars{
replace `v'pee_min=`v'pee_min/pp_`v'ee
replace `v'pee_max=`v'pee_max/pp_`v'ee
}
drop pp*

* apply interval regression to bounds from unfolding brackets
local vars gpee npee npse ese
foreach v of local vars{
replace `v'_min=0 if `v'_min==. & `v'_max!=.
replace `v'_max=. if `v'_max==. & `v'_min!=.
replace `v'_max=`v'_max+.01 if `v'_max==`v'_min
intreg `v'_min `v'_max
predict p`v' if `v'_min!=., ystar(`v'_min,`v'_max)
replace `v'_`a'=p`v' if p`v'!=. & `v'_`a'==.
drop p`v'
drop `v'_min `v'_max
}

* convert self employment income to weeks
replace npse_`a'=npse_`a'/52 
replace ese_`a'=ese_`a'/52 

********************************************************************************
* Part 6: hours per week for self-employed
********************************************************************************

ge hoursse_`a'=N8CJSEHS if N8CJSEHS>=0

********************************************************************************
* Part 7: inflation adjustment
********************************************************************************

local vars npee_`a' gpee_`a' ese_`a' npse_`a'
foreach v of local vars {
replace `v'=`v'/rpi
}

********************************************************************************
* Part 8: Economic activity
********************************************************************************

rename ND8ECACT EconAct
replace EconAct=. if EconAct<0

ge EE_`a'=(EconAct==1|EconAct==2) if EconAct!=. 
ge SE_`a'=(EconAct==3|EconAct==4) if EconAct!=. 


* clasify as non-working if report zero earnings
replace EE_`a'=0 if npee_`a'==0 & EE_`a'!=.
replace SE_`a'=0 if ese_`a'==0  & SE_`a'!=.
ge AE_`a'=EE_`a'+SE_`a'

ge UN_`a'=EconAct==5 if EconAct!=.

********************************************************************************
* Part 9: recode hours
********************************************************************************

* recode hours to missing if hours>112 or zero
replace hoursee_`a'=. if (hoursee_`a'>112|hoursee_`a'==0)
replace hoursse_`a'=. if (hoursse_`a'>112|hoursse_`a'==0)

replace hoursee_`a'=40 if EconAct==1 & hoursee_`a'==.
replace hoursee_`a'=20 if EconAct==2 & hoursee_`a'==.
replace hoursse_`a'=40 if EconAct==3 & hoursse_`a'==.
replace hoursse_`a'=20 if EconAct==4 & hoursse_`a'==.

replace hoursee_`a'=. if  npee_`a'==.
replace hoursse_`a'=. if  ese_`a'==.

* recode earnings and hours as missing if don't match current economic activity
replace npee_`a'=. if EE_`a'!=1
replace gpee_`a'=. if EE_`a'!=1
replace hoursee_`a'=. if  EE_`a'!=1
replace ese_`a'=. if SE_`a'!=1
replace hoursse_`a'=. if SE_`a'!=1

********************************************************************************
* Part 10: hourly wages
********************************************************************************

* employed net
ge wageee_`a'=npee_`a'/hoursee_`a'

* employed gross
ge gwageee_`a'=gpee_`a'/hoursee_`a'

* self-employed (earnings)
ge wagese_`a'=ese_`a'/hoursse_`a'

* self-employed (profit) 
ge prose_`a'=npse_`a'/hoursse_`a'

********************************************************************************
* Part 11: combine employed and self-employed
********************************************************************************

egen wage_`a'=rowmax(wageee_`a' wagese_`a')
drop wageee_`a' wagese_`a'

egen pay_`a'=rowmax(npee_`a' ese_`a')
drop npee_`a' ese_`a'

egen hours_`a'=rowmax(hoursee_`a' hoursse_`a')
drop hoursee_`a' hoursse_`a'

********************************************************************************
*  Part 12: log variables
********************************************************************************

local vars wage pay hours
foreach v of local vars {
ge l`v'_`a'=ln(`v'_`a')
}

********************************************************************************
* Part 13: SOC 1990 and 2000
********************************************************************************

ge soc90_`a'=N8SOC90 if N8SOC90>0
replace soc90_`a'=. if AE_`a'!=1

ge soc00_`a'=N8SOCX  if N8SOCX>0
replace soc00_`a'=. if AE_`a'!=1

********************************************************************************
* Part 14: Occupational attainment RGSC SC based on Occ 1990
********************************************************************************

rename N8SC SC_`a'
replace SC_`a'=. if SC_`a'<1|SC_`a'==6|AE_`a'!=1


********************************************************************************
* Part 15: Job security
********************************************************************************

rename N8JSECUE JobSecurity_`a'
replace JobSecurity_`a'=. if JobSecurity_`a'<0|AE_`a'!=1
ge JobSecurityHi_`a'=JobSecurity_`a'==1 if JobSecurity_`a'!=.
ge JobSecurityMedHi_`a'=JobSecurity_`a'==1|JobSecurity_`a'==2 if JobSecurity_`a'!=.


********************************************************************************
* Part 16: Industry
********************************************************************************

rename N8SIC3 Ind_`a' /* 2003 code */
replace Ind_`a'=. if Ind_`a'<0|AE_`a'!=1

********************************************************************************
* Part 16: Cognitive tests
********************************************************************************

* word recall
rename N8CFLISN N8CFLISN_`a'
rename N8CFLISD  N8CFLISD_`a'

* letter search (speed)
rename  N8CFCOR N8CFCOR_`a'
rename  N8CFMIS N8CFMIS_`a'
rename  N8CFTOT  N8CFTOT_`a'
rename  N8CFRC  N8CFRC_`a'

* animal naming task
rename N8CFANI N8CFANI_`a'


********************************************************************************
* Part 16: Education
********************************************************************************

rename  N8ACTAGL   agelsch_`a'
replace agelsch_`a'=. if agelsch_`a'<0
replace agelsch_`a'=16 if agelsch_`a'<16
replace agelsch_`a'=18 if agelsch_`a'>18 & agelsch_`a'!=.

********************************************************************************
* Part 17: IPIP
********************************************************************************

// all IPIP items
local IPIPvars N8SCQ2A N8SCQ2B N8SCQ2C N8SCQ2D N8SCQ2E N8SCQ2F N8SCQ2G N8SCQ2H N8SCQ2I N8SCQ2J N8SCQ2K N8SCQ2L N8SCQ2M N8SCQ2N N8SCQ2O N8SCQ2P N8SCQ2Q N8SCQ2R N8SCQ2S N8SCQ2T N8SCQ2U N8SCQ2V N8SCQ2W N8SCQ2X N8SCQ2Y N8SCQ2Z N8SCQ2AA N8SCQ2BB N8SCQ2CC N8SCQ2DD N8SCQ2EE N8SCQ2FF N8SCQ2GG N8SCQ2HH N8SCQ2II N8SCQ2JJ N8SCQ2KK N8SCQ2LL N8SCQ2MM N8SCQ2NN N8SCQ2OO N8SCQ2PP N8SCQ2QQ N8SCQ2RR N8SCQ2SS N8SCQ2TT N8SCQ2UU N8SCQ2VV N8SCQ2WW N8SCQ2XX

// reverse coded items
local ReverseCode N8SCQ2B N8SCQ2D N8SCQ2F N8SCQ2H N8SCQ2J N8SCQ2L N8SCQ2N N8SCQ2P N8SCQ2R N8SCQ2T N8SCQ2V N8SCQ2X N8SCQ2Z N8SCQ2BB N8SCQ2CC N8SCQ2DD N8SCQ2FF N8SCQ2HH N8SCQ2JJ N8SCQ2LL N8SCQ2MM N8SCQ2RR N8SCQ2TT N8SCQ2WW

// conscientiousness
local Cons N8SCQ2C N8SCQ2H N8SCQ2M N8SCQ2R N8SCQ2W N8SCQ2BB N8SCQ2GG N8SCQ2LL N8SCQ2QQ N8SCQ2VV

// agreeableness
local Agre N8SCQ2B N8SCQ2G N8SCQ2L N8SCQ2Q N8SCQ2V N8SCQ2AA N8SCQ2FF N8SCQ2KK N8SCQ2PP N8SCQ2UU

// emotional stability
local ES N8SCQ2D N8SCQ2I N8SCQ2N N8SCQ2S N8SCQ2X N8SCQ2CC N8SCQ2HH N8SCQ2MM N8SCQ2RR N8SCQ2WW

// extroversion
local EX  N8SCQ2A N8SCQ2F N8SCQ2K N8SCQ2P N8SCQ2U N8SCQ2Z N8SCQ2EE N8SCQ2JJ N8SCQ2OO N8SCQ2TT

// intellect
local Inte N8SCQ2E N8SCQ2J N8SCQ2O N8SCQ2T N8SCQ2Y N8SCQ2DD N8SCQ2II N8SCQ2NN N8SCQ2SS N8SCQ2XX

foreach v of local IPIPvars{
	replace `v' = . if `v' < 0
}

foreach v of local ReverseCode{
	replace `v' = 6 - `v'
}


egen nmCons = rownonmiss(`Cons')
egen nmAgre = rownonmiss(`Agre')
egen nmES = rownonmiss(`ES')
egen nmEX = rownonmiss(`EX')
egen nmInte = rownonmiss(`Inte')


egen Cons_`a' = rowtotal(`Cons') if nmCons==10
egen Agre_`a' = rowtotal(`Agre') if nmAgre==10
egen ES_`a' = rowtotal(`ES') if nmES==10
egen EX_`a' = rowtotal(`EX') if nmEX==10
egen Inte_`a' = rowtotal(`Inte') if nmInte==10

su   Cons Agre ES EX Inte
corr Cons Agre ES EX Inte


ge WEMWB_`a' = ND8WEMWB if ND8WEMWB>=0


********************************************************************************
* Finish up
********************************************************************************


keep NCDSID *_`a'
sort NCDSID
rename *_`a' *_I`a'

save scratchdata/ncds_2008_flatfile.dta, replace

